<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/12 0012
 * Time: 下午 3:55
 */



namespace ServiceClass;

use App\Http\Requests\Request;
use Illuminate\Support\Facades\DB;
use ToolClass\PageClass;

use ServiceClassInterface\CategoriesContract;

class Categories implements CategoriesContract
{
    public function addCategories($categoriesData)
    {
        $categoriesName = $categoriesData['categories_name'];

        $check = DB::table('categories')->where('categories_name','=',$categoriesName)->first();

        if($check){
            return "该分类已存在";
        }
        $categoriesData['categories_CreateTime'] = date('Y-m-d');
        $result = DB::table('categories')->insert($categoriesData);

        if($result){
            return 1;
        }else{
            return 0;
        }
    }

    public function deleteCategories($categoriesData)
    {

        $result = DB::table('categories')->where('categories_id',$categoriesData)->delete();
        if($result){
            return 1;
        }else{
            return 0;
        }
    }

    public function searchCategories($categoriesData)
    {

        $result = DB::table('categories')->where('categories_id',$categoriesData)->get();
        if($result){
            return $result;
        }else{
            return 0;
        }
    }

    public function updateCategories($categoriesData)
    {

        $categories_id = $categoriesData['categories_id'];

        $result = DB::table('categories')->where('categories_id',$categories_id)->update([
            'categories_name' => $categoriesData['categories_name'],
            'categories_introduce' => $categoriesData['categories_introduce']
        ]);

        if($result){
            return 1;
        }else{
            return "服务器错误，请重试";
        }
    }

    public function selectCategoriesList()
    {
        $result = DB::table('categories')->get();
        return $result;
    }
}